package cn.itcast.context;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


//用servletContext实现请求转发:mvc
//注意：
//1.转发之前的所有写入都无效
//2.转发之前，response不能提交，否则转发的时候服务器会抛：Cannot forward after response has been committed

public class ServletContextDemo4 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//String data = "<h1><font color='red'>abcddfdf</font></h1>";
		
		response.getOutputStream().write("1111".getBytes());
		
		ServletContext context = this.getServletContext();
		RequestDispatcher rd = context.getRequestDispatcher("/servlet/ServletContextDemo5");
		rd.forward(request, response);  //doget()
		
		response.getOutputStream().write("4444444444".getBytes());
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
